# 19_jes2001_2016_participation_predict.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-7-19 Taka-aki Asano
# Last Modified: 2022-1-4

# package
require("dplyr")
require("tidyr")
require("xtable")


# 2003
## merge
JES2003_Ideology <- JES2003_Respondent[,c("ID", "Ideology")]
JES2003_Ideology <- as.data.frame(JES2003_Ideology)
JES2003_Ideology <- left_join(
  JES2003_Ideology, 
  JES2003[,c("ID", "GENDER", "EDUCAT03", "BORNY", "E1", "E11_6", 
             "E11_7", "E11_10", "E11_2", "E11_3", "E11_4", 
             "E11_5", "E11_8", "E11_11", "E11_12", "D8")], 
  by = "ID")

## recode
JES2003_Ideology$Vote <- NA
JES2003_Ideology$Vote[JES2003_Ideology$E1 %in% c(1, 2)] <- 1
JES2003_Ideology$Vote[JES2003_Ideology$E1 == 3] <- 0
JES2003_Ideology$Campaign <- JES2003_Ideology$E11_6 + JES2003_Ideology$E11_7 + JES2003_Ideology$E11_10
JES2003_Ideology$System <- JES2003_Ideology$E11_2 + JES2003_Ideology$E11_3 + JES2003_Ideology$E11_4 + JES2003_Ideology$E11_5
JES2003_Ideology$Demo <- JES2003_Ideology$E11_8 + JES2003_Ideology$E11_11
JES2003_Ideology$Ideology_strong <- abs(JES2003_Ideology$Ideology - 
                                          median(JES2003_Ideology$Ideology, na.rm = TRUE))
JES2003_Ideology$D8[JES2003_Ideology$D8 %in% c(88, 99)] <- NA
JES2003_Ideology$Support <- ifelse(JES2003_Ideology$D8 == 9, 0, 1)
JES2003_Ideology$Female <- ifelse(JES2003_Ideology$GENDER == 2, 1, 0)
JES2003_Ideology$EDUCAT03[JES2003_Ideology$EDUCAT03 == 9] <- NA
JES2003_Ideology$College <- ifelse(JES2003_Ideology$EDUCAT03 == 4, 1, 0)
JES2003_Ideology$Age <- 2003 - JES2003_Ideology$BORNY

## regression
vote2003 <- glm(
  Vote ~ Ideology + Ideology_strong + Support + Female + College + Age, 
  data = JES2003_Ideology, family = binomial(link = "logit")
)
summary(vote2003)

campaign2003 <- glm(
  Campaign ~ Ideology + Ideology_strong + Support + Female + College + Age, 
  data = JES2003_Ideology, family = "poisson"
)
summary(campaign2003)

system2003 <- glm(
  System ~ Ideology + Ideology_strong + Support + Female + College + Age, 
  data = JES2003_Ideology, family = "poisson"
)
summary(system2003)

demo2003 <- glm(
  Demo ~ Ideology + Ideology_strong + Support + Female + College + Age, 
  data = JES2003_Ideology, family = "poisson"
)
summary(demo2003)

online2003 <- glm(
  E11_12 ~ Ideology + Ideology_strong + Support + Female + College + Age, 
  data = JES2003_Ideology, family = binomial(link = "logit")
)
summary(online2003)

## predict
median2003 <- median(JES2003_Ideology$Ideology, na.rm = TRUE)
max2003 <- median(JES2003_Ideology$Ideology, na.rm = TRUE) + 
  sd(JES2003_Ideology$Ideology, na.rm = TRUE)
min2003 <- median(JES2003_Ideology$Ideology, na.rm = TRUE) - 
  sd(JES2003_Ideology$Ideology, na.rm = TRUE)
predict2003 <- data.frame(
  Ideology = c(min2003, median2003, max2003), 
  Ideology_strong = c(sd(JES2003_Ideology$Ideology, na.rm = TRUE), 0, sd(JES2003_Ideology$Ideology, na.rm = TRUE)), 
  Support = rep(0, 3), 
  Female = rep(0, 3), 
  College = rep(1, 3), 
  Age = rep(mean(JES2003_Ideology$Age, na.rm = TRUE), 3)
)
predict2003$Vote <- predict(vote2003, newdata = predict2003, type = "response")
predict2003$Campaign <- predict(campaign2003, newdata = predict2003, type = "response")
predict2003$System <- predict(system2003, newdata = predict2003, type = "response")
predict2003$Demo <- predict(demo2003, newdata = predict2003, type = "response")
predict2003$Online <- predict(online2003, newdata = predict2003, type = "response")


# 2016
## merge
JES2016_Ideology <- JES2016_Respondent[,c("id", "Ideology")]
JES2016_Ideology <- as.data.frame(JES2016_Ideology)
JES2016_Ideology <- left_join(
  JES2016_Ideology, 
  JES2016[,c("id", "b_F1_SEX", "F4", "b_F2_AGE", "b_Q1", "b_Q12_6", 
             "b_Q12_7", "b_Q12_10", "b_Q12_2", "b_Q12_3", "b_Q12_4", 
             "b_Q12_5", "b_Q12_8", "b_Q12_11", "b_Q12_12", "Q5")], 
  by = "id")

## recode
JES2016_Ideology$Vote <- NA
JES2016_Ideology$Vote[JES2016_Ideology$b_Q1 %in% c(1, 2)] <- 1
JES2016_Ideology$Vote[JES2016_Ideology$b_Q1 == 3] <- 0
JES2016_Ideology$Campaign <- JES2016_Ideology$b_Q12_6 + JES2016_Ideology$b_Q12_7 + JES2016_Ideology$b_Q12_10
JES2016_Ideology$System <- JES2016_Ideology$b_Q12_2 + JES2016_Ideology$b_Q12_3 + JES2016_Ideology$b_Q12_4 + JES2016_Ideology$b_Q12_5
JES2016_Ideology$Demo <- JES2016_Ideology$b_Q12_8 + JES2016_Ideology$b_Q12_11
JES2016_Ideology$Ideology_strong <- abs(JES2016_Ideology$Ideology - 
                                          median(JES2016_Ideology$Ideology, na.rm = TRUE))
JES2016_Ideology$Support <- ifelse(JES2016_Ideology$Q5 == 7, 0, 1)
JES2016_Ideology$Female <- ifelse(JES2016_Ideology$b_F1_SEX == 2, 1, 0)
JES2016_Ideology$F4[JES2016_Ideology$F4 == 9] <- NA
JES2016_Ideology$College <- ifelse(JES2016_Ideology$F4 == 4, 1, 0)

## regression
vote2016 <- glm(
  Vote ~ Ideology + Ideology_strong + Support + Female + College + b_F2_AGE, 
  data = JES2016_Ideology, family = binomial(link = "logit")
)
summary(vote2016)

campaign2016 <- glm(
  Campaign ~ Ideology + Ideology_strong + Support + Female + College + b_F2_AGE, 
  data = JES2016_Ideology, family = "poisson"
)
summary(campaign2016)

system2016 <- glm(
  System ~ Ideology + Ideology_strong + Support + Female + College + b_F2_AGE, 
  data = JES2016_Ideology, family = "poisson"
)
summary(system2016)

demo2016 <- glm(
  Demo ~ Ideology + Ideology_strong + Support + Female + College + b_F2_AGE, 
  data = JES2016_Ideology, family = "poisson"
)
summary(demo2016)

online2016 <- glm(
  b_Q12_12 ~ Ideology + Ideology_strong + Support + Female + College + b_F2_AGE, 
  data = JES2016_Ideology, family = binomial(link = "logit")
)
summary(online2016)

## predict
median2016 <- median(JES2016_Ideology$Ideology, na.rm = TRUE)
max2016 <- median(JES2016_Ideology$Ideology, na.rm = TRUE) + 
  sd(JES2016_Ideology$Ideology, na.rm = TRUE)
min2016 <- median(JES2016_Ideology$Ideology, na.rm = TRUE) - 
  sd(JES2016_Ideology$Ideology, na.rm = TRUE)
predict2016 <- data.frame(
  Ideology = c(min2016, median2016, max2016), 
  Ideology_strong = c(sd(JES2016_Ideology$Ideology, na.rm = TRUE), 0, sd(JES2016_Ideology$Ideology, na.rm = TRUE)), 
  Support = rep(0, 3), 
  Female = rep(0, 3), 
  College = rep(1, 3), 
  b_F2_AGE = rep(mean(JES2016_Ideology$b_F2_AGE, na.rm = TRUE), 3)
)
predict2016$Vote <- predict(vote2016, newdata = predict2016, type = "response")
predict2016$Campaign <- predict(campaign2016, newdata = predict2016, type = "response")
predict2016$System <- predict(system2016, newdata = predict2016, type = "response")
predict2016$Demo <- predict(demo2016, newdata = predict2016, type = "response")
predict2016$Online <- predict(online2016, newdata = predict2016, type = "response")


# output
predict2003_2016 <- data.frame(
  Voter = c("Median Voter - 1SD", "Median Voter", "Median Voter + 1SD", 
            "Median Voter - 1SD", "Median Voter", "Median Voter + 1SD"), 
  Year = c(2003, 2003, 2003, 2016, 2016, 2016), 
  Vote = c(predict2003[1, "Vote"], predict2003[2, "Vote"], 
           predict2003[3, "Vote"], predict2016[1, "Vote"], 
           predict2016[2, "Vote"], predict2016[3, "Vote"]), 
  Campaign = c(predict2003[1, "Campaign"], predict2003[2, "Campaign"], 
               predict2003[3, "Campaign"], predict2016[1, "Campaign"], 
               predict2016[2, "Campaign"], predict2016[3, "Campaign"]), 
  `System-Affirming` = c(predict2003[1, "System"], predict2003[2, "System"], 
                         predict2003[3, "System"], predict2016[1, "System"], 
                         predict2016[2, "System"], predict2016[3, "System"]), 
  `Elite-Challenging` = c(predict2003[1, "Demo"], predict2003[2, "Demo"], 
                          predict2003[3, "Demo"], predict2016[1, "Demo"], 
                          predict2016[2, "Demo"], predict2016[3, "Demo"]), 
  Online = c(predict2003[1, "Online"], predict2003[2, "Online"], 
             predict2003[3, "Online"], predict2016[1, "Online"], 
             predict2016[2, "Online"], predict2016[3, "Online"])
)
